package cn.rokhdelar.umbrsapi.management.controller;

import cn.rokhdelar.umbrsapi.management.entity.SysDepartment;
import cn.rokhdelar.umbrsapi.management.service.SysDepartmentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/management/departments")
public class SysDepartmentController {
    private final SysDepartmentService sysDepartmentService;

    public SysDepartmentController(SysDepartmentService sysDepartmentService) {
        this.sysDepartmentService = sysDepartmentService;
    }

    @GetMapping
    public Page<SysDepartment> list(@RequestParam(value = "name", defaultValue = "") String name,
                                    @RequestParam(value = "current", defaultValue = "0") Integer current,
                                    @RequestParam(value = "size", defaultValue = "20") Integer size) {
        PageRequest pageRequest = PageRequest.of(current,size);
        return sysDepartmentService.list(name, pageRequest);
    }

    @GetMapping(value = "/{id}")
    public SysDepartment get(@PathVariable(value = "id") Long id) {
        return sysDepartmentService.get(id);
    }

    @PostMapping
    public SysDepartment create(@RequestBody SysDepartment sysUser) {
        return sysDepartmentService.save(sysUser);
    }

    @PatchMapping(value = "/{id}")
    public SysDepartment update(@RequestBody SysDepartment sysUser) {
        return sysDepartmentService.save(sysUser);
    }

    @DeleteMapping(value = "/{id}")
    public void remove(@PathVariable(value = "id") Long id) {
        sysDepartmentService.delete(id);
    }
}
